package com.ldc.login

import com.alibaba.android.arouter.facade.annotation.Route
import com.ldc.commonlibs.conf.RouteConf
import com.ldc.commonlibs.uts.DataStoreUts
import com.ldc.commonlibs.uts.KeyboardUtils
import com.ldc.commonlibs.uts.ViewModelUtils
import com.ldc.commonres.common.VBMActivity
import com.ldc.login.databinding.LoginActivityLoginBinding
import com.ldc.login.viewodel.LoginViewModel

@Route(path = RouteConf.LoginRoute.startEnter)
class LoginActivity : VBMActivity<LoginActivityLoginBinding, LoginViewModel>() {
    override fun main() {
        viewBind?.let { vb ->
            vb.topBar.tvName.text = "登录"
            vb.btnLogin.setOnClickListener {
                val name = viewBind?.etUserName?.text.toString()
                val password = viewBind?.etPassword?.text.toString()
                login(name, password).run {
                    // 隐藏键盘
                    KeyboardUtils.hideSoftInput(activity!!)
                }
            }
            vb.topBar.let { top ->
                top.flBack.setOnClickListener { finish() }
            }

            if (DataStoreUts.INSTANCE.getIsLogin()) {
                vb.etUserName.setText(DataStoreUts.INSTANCE.getUserName())
                vb.etPassword.setText(DataStoreUts.INSTANCE.getUserPassWord())
            }
        }
    }

    override fun createViewBinding(): LoginActivityLoginBinding =
        LoginActivityLoginBinding.inflate(layoutInflater)

    override fun createViewModel(): LoginViewModel =
        ViewModelUtils.create(this, LoginViewModel::class.java)


    //登录
    private fun login(name: String, password: String) {
        viewModel?.login(name, password)
    }

}